const Router = require("koa-router");
const routes = require("./adminRoutes");
// 单独处理的路由
const indexRouter = require("./indexRouter");

const initRoutes = app => {
  // 单独处理路由
  app.use(indexRouter.routes());
  // 循环路由列表;
  routes.forEach(item => {
    let router;
    if (item.prefix) {
      router = new Router({
        prefix: "/admin/" + item.prefix
      });
    } else {
      router = new Router();
    }
    const service = require(`../adminService/${item.service ||
      item.prefix}Service`);
    item.interfaces.forEach(interface => {
      // 循环生成接口
      router.all("/" + interface, async ctx => {
        const res = await service[interface](ctx);
        ctx.body = res;
      });
    });
    app.use(router.routes());
  });
};

module.exports = initRoutes;